package com.ruoyi.web.controller.practice;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.PracticeReport;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.service.IPracticeReportService;
import com.ruoyi.web.controller.common.CommonController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;


@Controller
@RequestMapping("/practice/report")
public class ReportController extends BaseController {

    private String prefix = "practice/report";

    @Autowired
    private IPracticeReportService reportService;

    @RequiresPermissions("practice:report:view")
    @GetMapping()
    public String report(ModelMap mmap)
    {
        mmap.put("tasks", reportService.selectReportList());
        return prefix + "/report";
    }

    @Autowired
    CommonController controller;

    @PostMapping("/upload")
    @ResponseBody
    public AjaxResult upload(String fileName,MultipartFile file) throws Exception {

        AjaxResult ajaxResult=controller.uploadFile(file);
        if(ajaxResult.isSuccess()){
            String url=ajaxResult.get("fileName").toString();
            PracticeReport practiceReport= new PracticeReport();
            practiceReport.setName(getSysUser().getUserName()+'-'+fileName);
            practiceReport.setUrl(url);
            practiceReport.setUserId(getSysUser().getUserId());
            practiceReport.setDeptId(getSysUser().getDeptId());
            reportService.insertReport(practiceReport);
        }
        return ajaxResult;
    }
}
